package com.example.homework9;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

public class Fragment3_1 extends Fragment {
    private Button button1, button2;
    private TextView nameTextView, ageTextView, contentTextView;
    private String username;
    private int age;
    public Fragment3_1(){
        super(R.layout.fragment3_1);
    }

    public void onViewCreated(View view, Bundle savedInstanceState){
        super.onViewCreated(view,savedInstanceState);

        nameTextView = getView().findViewById(R.id.name);
        ageTextView = getView().findViewById(R.id.age);
        contentTextView = getView().findViewById(R.id.contentTextView);

        button1 = getView().findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                username = nameTextView.getText().toString();
                age = Integer.parseInt(ageTextView.getText().toString());

                DbHelper dbHelper = new DbHelper(getContext());
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("username", username);
                values.put("age", age);
                long ret = db.insert("users",null,values);
                String msg = ret == -1 ? "添加失败！" : "添加成功";
                Toast.makeText(getActivity(),msg,Toast.LENGTH_SHORT).show();
                db.close();

                SQLiteDatabase db1 = dbHelper.getReadableDatabase();
                StringBuilder stringBuilder = new StringBuilder();
                Cursor cursor = db1.query("users",null,"",null,null,null,null);
                while(cursor.moveToNext()){
                    long id = cursor.getLong(cursor.getColumnIndex("id"));
                    String username = cursor.getString(cursor.getColumnIndex("username"));
                    int age = cursor.getInt(cursor.getColumnIndex("age"));
                    stringBuilder.append("id: ").append(id)
                            .append("  username: ").append(username)
                            .append("  age: ").append(age)
                            .append("\n");
                }
                contentTextView.setText(stringBuilder.toString());
            }
        });

        button2 = getView().findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                DbHelper dbHelper = new DbHelper(getContext());
                SQLiteDatabase db = dbHelper.getReadableDatabase();
                StringBuilder stringBuilder = new StringBuilder();
                Cursor cursor = db.query("users",null,"age<18",null,null,null,null);
                while(cursor.moveToNext()){
                    long id = cursor.getLong(cursor.getColumnIndex("id"));
                    String username = cursor.getString(cursor.getColumnIndex("username"));
                    int age = cursor.getInt(cursor.getColumnIndex("age"));
                    stringBuilder.append("id: ").append(id)
                            .append("  username: ").append(username)
                            .append("  age: ").append(age)
                            .append("\n");
                }
                contentTextView.setText(stringBuilder.toString());
            }
        });

    }
}